package com.agua.enturno.vista.modelos;

import com.agua.enturno.servicios.AgendaService;
import com.agua.enturno.vista.dto.AgendaDTO;
import com.agua.enturno.vista.dto.HorarioFullDTO;
import org.apache.wicket.injection.Injector;
import org.apache.wicket.model.IModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.joda.time.*;

import java.util.List;


public class IntervaloDeHorariosModel implements IModel<List<HorarioFullDTO>> {
	
	private static final long serialVersionUID = 1L;
	
	@SpringBean
	private AgendaService agendaService;
	
	private IModel<AgendaDTO> agendaModel;
	private Interval intervalo;
	
	public IntervaloDeHorariosModel(IModel<AgendaDTO> agendaModel) {
		
		this.agendaModel = agendaModel;
		this.setToCurrentDay();
		
		Injector.get().inject(this);
	}

	@Override
	public void detach() {
		agendaModel.detach();
	}

	@Override
	public List<HorarioFullDTO> getObject() {
		return agendaService.findHorariosFullByInterval(agendaModel.getObject().getId(), intervalo);
	}

	@Override
	public void setObject(List<HorarioFullDTO> object) {
		
	}
	
	public void setToCurrentDay() {
		this.intervalo = LocalDate.now().toInterval();
	}
	
	public void setToCurrentWeek() {
		
		DateTime from = LocalDate.now().withDayOfWeek(DateTimeConstants.MONDAY).toDateTimeAtStartOfDay();
		DateTime to = from.plusWeeks(1);
		
		this.intervalo = new Interval(from,to);
	}
	
	public void setToCurrentMonth() {
		this.intervalo = YearMonth.now().toInterval();
	}
	
}
